package com.taiyi.renaissancemall.net;

import android.util.Log;


import com.lzy.okgo.OkGo;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;

import java.io.File;
import java.util.Locale;
import java.util.Map;


/**
 * Created by AccToken on 2018/4/2.
 * app网络请求管理类
 */
public class HttpUtils {


    public static String language = "";

    private static String lang;

    private static void getAppLanguage() {
        if (Utils.getSpUtils().getInt("language") == -1) {
            Locale locale = Utils.getSystemLocale();
            if (locale.getLanguage().equalsIgnoreCase(new Locale("zh").getLanguage())) {
                lang = "zh";
            } else if (locale.getLanguage().equalsIgnoreCase(new Locale("en").getLanguage())) {
                lang = "en";
            }
        } else if (Utils.getSpUtils().getInt("language") == 1) {
            lang = "zh";
        } else if (Utils.getSpUtils().getInt("language") == 2) {
            lang = "en";
        }
    }

    /**
     * Gets requets.
     *
     * @param <T>      the type parameter
     * @param url      the url
     * @param tag      the tag
     * @param map      the map
     * @param callback the callback
     */
    public static <T> void getRequest(String url, Object tag, Map<String, String> map, JsonCallback<T> callback) {
        getAppLanguage();
        String token = Utils.getSpUtils().getString("token");
        OkGo.<T>get(url)
                .tag(tag)
                .headers("langu", lang)
                .headers("Authorization", token)
//                .headers("Authorization", Utils.getSpUtils().getString(Constants.TOKEN, ""))
                .params(map)
                .execute(callback);
    }


    /**
     * Post request.
     *
     * @param <T>      the type parameter
     * @param url      the url
     * @param tag      the tag
     * @param map      the map
     * @param callback the callback
     */
    public static <T> void postRequest(String url, Object tag, Map<String, String> map, JsonCallback<T> callback) {
        getAppLanguage();
        String token = Utils.getSpUtils().getString("token");
        Log.i("token", "----" + token);
        OkGo.<T>post(url)
                .tag(tag)
                .headers("Content-Type", "application/json")
                .headers("Authorization", token)
                .headers("langu", lang)
//                .headers("Authorization", Utils.getSpUtils().getString(Constants.TOKEN, ""))
                .params(map)
                .execute(callback);
    }

    /**
     * Post request.
     *
     * @param <T>      the type parameter
     * @param url      the url
     * @param tag      the tag
     * @param parms    the parms
     * @param callback the callback
     */
    public static <T> void postRequest(String url, Object tag, String parms, JsonCallback<T> callback) {
//        String lang = "";
////        if (Utils.getSpUtils().getString("language", "").equals("")) {
////            lang = language;
////        } else {
////            lang = Utils.getSpUtils().getString("language", "");
////        }
        getAppLanguage();
        String token = Utils.getSpUtils().getString("token");
        OkGo.<T>post(url)
//                .headers("Authorization", Utils.getSpUtils().getString(Constants.TOKEN, ""))
                .headers("Content-Type", "application/json")
                .headers("Authorization", token)
                .headers("langu", lang)
                .tag(tag)
                .upJson(parms)
                .execute(callback);
    }

    /**
     * Post request.
     *
     * @param <T>      the type parameter
     * @param url      the url
     * @param tag      the tag
     * @param map      the map
     * @param callback the callback
     */
    public static <T> void putRequest(String url, Object tag, Map<String, String> map, JsonCallback<T> callback) {
        getAppLanguage();
        String token = Utils.getSpUtils().getString("token");
        OkGo.<T>put(url)
                .tag(tag)
                .headers("Authorization", token)
                .headers("langu", lang)
//                .headers("Authorization", Utils.getSpUtils().getString(Constants.TOKEN, ""))
                .params(map)
                .execute(callback);
    }


    public static <T> void uploadFileList(String url, Object tag, String map, File file1, File file2, File file3, JsonCallback<T> callback) {
        String token = Utils.getSpUtils().getString("token");
        getAppLanguage();
        OkGo.<T>post(url)
                .tag(tag)
                .headers("Authorization", token)
                .headers("langu", lang)
                .params("frontPhoto", file1)
                .params("reversePhoto", file2)
                .params("holdPhoto", file3)
                .isMultipart(true)
                .execute(callback);
    }

    public static <T> void uploadFile(String url, Object tag, Map<String, String> map, String key, File file, JsonCallback<T> callback) {
        String token = Utils.getSpUtils().getString("token");
        getAppLanguage();
        String type = map.get("type");
        if ("0".equals(type)) {

            OkGo.<T>post(url)
                    .tag(tag)
                    .params(map)
                    .headers("Authorization", token)
                    .headers("langu", lang)
                    .params(key, file)
                    .isMultipart(true)
                    .execute(callback);
        } else {
            OkGo.<T>post(url)
                    .tag(tag)
                    .params(map)
                    .headers("Authorization", token)
                    .headers("langu", lang)
//                    .params(key, file)
                    .isMultipart(true)
                    .execute(callback);
        }
    }

}
